package cn.liziguo.tank.util;

import com.almasb.fxgl.core.math.FXGLMath;
import com.almasb.fxgl.dsl.FXGL;
import com.almasb.fxgl.entity.Entity;
import javafx.geometry.Point2D;

/**
 * @author Liziguo
 * @date 2022-07-12 22:21:24
 */
public final class CoordinateUtils {

//    public static final double DEG_TO_RAD = Math.PI / 180;
//    public static final double RAD_TO_DEG = 180 / Math.PI;

    private CoordinateUtils() {
    }

    /**
     * 角度制转弧度制
     *
     * @param deg 角度
     * @return 弧度
     */
    public static double deg2Rad(double deg) {
        return Math.toRadians(deg);
    }

    /**
     * 弧度制转角度制
     *
     * @param rad 弧度
     * @return 角度
     */
    public static double rad2Deg(double rad) {
        return Math.toDegrees(rad);
    }

    /**
     * 计算点(x1, y1)绕点(x2 ,y2)旋转angle度后的坐标
     *
     * @param x1    x1
     * @param y1    y1
     * @param x2    x2
     * @param y2    y2
     * @param angle 旋转角(弧度制)
     * @return 旋转后的坐标
     */
    public static Point2D rotate(double x1, double y1, double x2, double y2, double angle) {
        // x = (x1 - x2)* cos(θ) - (y1 - y2)* sin(θ) + x2 ;
        // y = (x1 - x2)* sin(θ) + (y1 - y2)* cos(θ) + y2 ;
        final double diffX = x1 - x2;
        final double diffY = y1 - y2;
        final double sin = Math.sin(angle);
        final double cos = Math.cos(angle);
        return new Point2D(diffX * cos - diffY * sin + x2, diffX * sin + diffY * cos + y2);
    }

    /**
     * 本地坐标转世界坐标(数学坐标系中)
     *
     * @param entity 使用谁的本地坐标系
     * @param x      本地坐标x
     * @param y      本地坐标y
     * @return 世界坐标(x, y)
     */
    @Deprecated
    public static Point2D local2WordMath(Entity entity, double x, double y) {
        final Point2D center = entity.getCenter();
        final double ex = center.getX();
        final double ey = center.getY();
        return rotate(ex + x, ey + y, ex, ey, -deg2Rad(entity.getRotation()));
    }

    /**
     * 世界坐标转本地坐标(数学坐标系中)
     *
     * @param entity 使用谁的本地坐标系
     * @param x      世界坐标x
     * @param y      世界坐标y
     * @return 本地坐标(x, y)
     */
    @Deprecated
    public static Point2D word2LocalMath(Entity entity, double x, double y) {
        final Point2D center = entity.getCenter();
        final double ex = center.getX();
        final double ey = center.getY();
        final Point2D rotate = rotate(x, y, ex, ey, deg2Rad(entity.getRotation()));
        return new Point2D(rotate.getX() - ex, rotate.getY() - ey);
    }

    public static Point2D local2Word(Entity entity, double x, double y) {
        final Point2D center = entity.getCenter();
        final double ex = center.getX();
        final double ey = center.getY();
        return rotate(ex + x, ey + y, ex, ey, deg2Rad(entity.getRotation()));
    }

    public static Point2D word2Local(Entity entity, double x, double y) {
        final Point2D center = entity.getCenter();
        final double ex = center.getX();
        final double ey = center.getY();
        Point2D rotate = rotate(x, y, ex, ey, -deg2Rad(entity.getRotation()));
        return new Point2D(rotate.getX() - ex, rotate.getY() - ey);
    }

    public static void main(String[] args) {
        System.out.println(FXGLMath.atan2Deg(2,1));
        Entity entity = FXGL.entityBuilder().at(2, 2).rotate(-90).build();
        System.out.println(entity.getTransformComponent().getAngle());
        System.out.println(local2Word(entity, 0, -1));
        System.out.println(local2Word(entity, 0, 1));
        System.out.println(local2Word(entity, -2, 2));
        System.out.println(local2Word(entity, 2, 2));
        System.out.println("---");
        System.out.println(word2Local(entity, 1, 2));
        System.out.println(word2Local(entity, 2, 3));
        System.out.println(word2Local(entity, 3, 2));
        System.out.println(word2Local(entity, 0, 0));
    }
}
